package com.kuang.bq;

import java.util.Collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * Author: Zhang Dongwei
 * Date: 2022/3/13 17:53
 *
 */
public class Test {
    public static void main(String[] args) throws InterruptedException {
        test4();
    }
    /**
     * 抛出异常
     * add、remove、element
     */
     public static void test1(){
        // 队列的大小
         ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
         System.out.println(blockingQueue.add("a"));
         System.out.println(blockingQueue.add("b"));
         System.out.println(blockingQueue.add("c"));
//        抛出异常 java.lang.IllegalStateException: Queue full,队列已满
//         System.out.println(blockingQueue.add("d"));
         System.out.println(blockingQueue.element());// 查看队首元素是谁

         System.out.println(blockingQueue.remove());
         System.out.println(blockingQueue.remove());
         System.out.println(blockingQueue.remove());
//        如果移除一个，这也会造成 java.util.NoSuchElementException 队列已空，没有元素可以取了
//         System.out.println(blockingQueue.remove());
     }

    /**
     * 有返回值，不抛出异常，offer、poll、peek
     */
    public static void test2(){
        // 队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.peek());
//        添加不会抛出异常，返回false
//        System.out.println(blockingQueue.offer("d"));
        System.out.println("=================");

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
//        移除不会抛出异常，返回null
        System.out.println(blockingQueue.poll());
    }

    /**
     * 等待，一直阻塞
     */
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);
//      一直阻塞，不会返回值
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
//        blockingQueue.put("d"); // 队列没有位置了，一直阻塞

//        若队列已满，再添加，则阻塞等待添加,返回被取出的值
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
//        移除不会抛出异常，返回null
//        System.out.println(blockingQueue.take());// 一直阻塞，程序一直在等待
    }
    /**
     * 超时等待
     * 这种情况也会发生阻塞等待，但会超时结束
     */
    public static void test4() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
        System.out.println("开始等待");
        blockingQueue.offer("d",2, TimeUnit.SECONDS);// 等待超过2秒就退出
        System.out.println("等待结束");
        System.out.println("=================取值=====================");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println("取值开始等待");
        blockingQueue.poll(2,TimeUnit.SECONDS);//超过两秒，我们就不等待了
        System.out.println("取值结束等待");
    }

}
